package com.example.myspringbootpj.controller;


import com.example.myspringbootpj.entity.TranslatedWord;
import com.example.myspringbootpj.entity.Word;
import com.example.myspringbootpj.entity.WordQuizDTO;
import com.example.myspringbootpj.mapper.WordMapper;
import com.example.myspringbootpj.service.StudyService;
import com.example.myspringbootpj.service.WordService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/getWords")
public class WordController {
private WordMapper wordMapper;
    private WordService wordService;
    @Autowired
    private StudyService studyService;

    @Autowired
    public WordController(WordService wordService , WordMapper wordMapper) {
        this.wordService = wordService;
        this.wordMapper = wordMapper;
    }

//    //测试测试 MyBatis-Plus 是否能正常插入和生成主键：
//    @PostMapping("/insert")
//    public String insertWord(@RequestParam("wordText") String wordText) {
//        wordService.insertWord(wordText); // 调用插入方法
//        return "Word inserted: " + wordText;
//    }

    //查看单词时，将单词放入新学单词表
    @PostMapping("/add-new-words")
    public ResponseEntity<String> addNewWords(HttpSession session) {
        String userId = (String) session.getAttribute("userID");

        if (userId == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户未登录喵");
        }

        studyService.addWordsToNewStudy(userId);
        return ResponseEntity.ok("新单词添加了，666");
    }

    // 获取翻译后的单词测试数据
    @GetMapping("/quiz")
    public List<WordQuizDTO> getWordQuiz(@RequestParam(defaultValue = "zh") String targetLanguage,HttpSession session) {
        return wordService.getWordQuiz( session,targetLanguage);
    }

    //将过的单词和id传入review表
    @PostMapping("/increment-study")
    public ResponseEntity<String> incrementStudyNum(@RequestParam("wordId") Integer wordId,HttpSession session) {
        // 从 session 中获取 userId
        String userId = (String) session.getAttribute("userID");
        if (userId == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户未登录捏");
        }
    studyService.addWordNum(userId, wordId);
        return ResponseEntity.ok("成功了，✌");
    }

    //收藏单词
    @PostMapping("/collection")
    public ResponseEntity<String> collectionWord(@RequestParam("wordId") Integer wordId,HttpSession session) {
        String userId = (String) session.getAttribute("userID");
        if (userId == null) {
            return ResponseEntity.ok("用户未登录喵");
        }
        wordMapper.insertCollection(userId, wordId);
        return ResponseEntity.ok("加入收藏成功");
    }

    //获取收藏的单词
    @GetMapping("/getCollection")
    public ResponseEntity<List<TranslatedWord>> getCollection(HttpSession session, @RequestParam(defaultValue = "zh") String targetLanguage ) {

        String userId = (String) session.getAttribute("userID");

        return wordService.getCollectionWord(session,targetLanguage);
    }

}
